import {createRouter, createWebHistory} from 'vue-router'
import {getToken} from "@/api/login.ts";


const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: "/login"
        },
        {
            path: "/login",
            name: "login",
            component: () => import("@/view/login/index.vue"),
            meta: {
                requireAuth: false //不需要登录验证
            }
        },
        {
            path: "/index",
            name: "index",
            component: () => import("@/view/index/index.vue"),
            meta: {
                requireAuth: true //需要登录验证
            }
        },
        {
            path: "/backend",
            name: "backend",
            redirect: "/backend/user",
            component: () => import("@/view/backend/index.vue"),
            meta: {
                requireAuth: true
            },
            children: [
                {
                    path: "user",
                    name: "user",
                    component: () => import("@/view/backend/user/index.vue"),
                    meta: {
                        requireAuth: true,
                        title: "用户管理"
                    },
                },
                {
                    path: "table",
                    name: "table",
                    component: () => import("@/view/backend/table/index.vue"),
                    meta: {
                        requireAuth: true,
                        title: "数据库表管理"
                    }
                },
                {
                    path: "schedule",
                    name: "schedule",
                    component: () => import("@/view/backend/schedule/index.vue"),
                    meta: {
                        requireAuth: true,
                        title: "调度管理"
                    }
                }
                ,
                {
                    path: "data",
                    name: "data",
                    component: () => import("@/view/backend/data/index.vue"),
                    meta: {
                        requireAuth: true,
                        title: "数据管理"
                    }
                },
                {
                    path:"model",
                    name:"model",
                    component:()=>import("@/view/backend/model/index.vue"),
                    meta:{
                        requireAuth:true,
                        title:"模型管理"
                    }
                },
                {
                    path:"train",
                    name:"train",
                    component:()=>import("@/view/backend/train/index.vue"),
                    meta:{
                        requireAuth: true,
                        title:"模型训练"
                    }
                }
            ]
        }
    ]
})
router.beforeEach((to, _from, next) => {
    if (to.meta.requireAuth) {
        let token = getToken();
        if (!token.value) {
            next("/login");
        } else {
            next()
        }
    } else {
        next();
    }
})
export default router
